﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework_11th
{
    public partial class Form1 : Form
    {
        private int wordNum;//当前正在处理的单词项的序号
        private int wordTotal;//本轮测试的单词总数
        private Answer[] answerArr;//存储单词项的数组
        private string[] wordFilledArr;//存储用户填写结果的数组
        private string[] resultArr;//存储批改结果的数组
        public Form1()
        {
            InitializeComponent();
            wordNum = 0;
            wordTotal = 0;
            this.comboBoxHowMany.Items.Add("10");
            this.comboBoxHowMany.Items.Add("20");
            this.comboBoxHowMany.Items.Add("35");
            this.comboBoxHowMany.Items.Add("50");
            this.comboBoxHowMany.Items.Add("75");
            //this.comboBoxHowMany.Items.Add("75");
            ToStateStart();            
        }

        /// <summary>
        /// “开始程序”按钮，点击后将进入到复习预热状态，
        /// 并处理程序入口阶段的相关参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonStart_Click(object sender, EventArgs e)
        {
            wordNum = 1;;
            wordTotal = int.Parse(this.comboBoxHowMany.Text);
            resultArr = new string[wordTotal];
            wordFilledArr = new string[wordTotal];
            answerArr = GenerateTestWords.RandomWordListFromDatabase(wordTotal);
            ToStateReview();
        }

        /// <summary>
        /// 从测试结束阶段返回开始程序阶段的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonReturnStart_Click(object sender, EventArgs e)
        {
            ToStateStart();
        }
        
        /// <summary>
        /// 从复习预热阶段进入开始测试阶段的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEndReview_Click(object sender, EventArgs e)
        {
            ToStateTest();
        }

        /// <summary>
        /// 测试过程中的“上一个”按钮，方便用户修改答案
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonPrior_Click(object sender, EventArgs e)
        {
            testOneWord(wordNum-1,this.textBoxEnglish.Text);
            if (wordNum > 1)
            {
                wordNum--;
                this.textBoxEnglish.Text = wordFilledArr[wordNum-1];
            }
            this.labelChinese.Text = wordNum.ToString() + '/' + wordTotal.ToString()
                                        +"\r\n"+answerArr[wordNum-1].Chinese;
        }

        /// <summary>
        /// 测试过程中的“下一个”按钮，
        /// 当前是最后一个单词时，点击该按钮将会进入测试结束阶段
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonNext_Click(object sender, EventArgs e)
        {
            testOneWord(wordNum-1,this.textBoxEnglish.Text);
            wordNum++;
            if (wordNum > wordTotal)
            {
                ToStateFinish();
                return;
            }
            this.textBoxEnglish.Text = wordFilledArr[wordNum-1];
            this.labelChinese.Text = wordNum.ToString() + '/' + wordTotal.ToString()
                                        + "\r\n" + answerArr[wordNum - 1].Chinese;
        }

        /// <summary>
        /// 开始程序阶段，
        /// 将会设置各个组件的可见性，模拟页面转换的效果
        /// </summary>
        private void ToStateStart()
        {
            this.comboBoxHowMany.Text = "20";
            this.comboBoxHowMany.Visible = true;
            this.labelHowMany.Visible = true;
            this.buttonStart.Visible = true;

            this.buttonEndReview.Visible = false;
            this.labelReview.Visible = false;

            this.labelChinese.Visible = false;
            this.textBoxEnglish.Visible = false;
            this.buttonNext.Visible = false;
            this.buttonPrior.Visible = false;

            this.textBoxResult.Visible = false;
            this.labelFinalResult.Visible = false;
            this.buttonReturnStart.Visible = false;
        }

        /// <summary>
        /// 复习预热阶段，
        /// 将会设置各个组件的可见性，模拟页面转换的效果，
        /// 期间连接数据库，获取本次测试的内容
        /// </summary>
        private void ToStateReview()
        {
            this.textBoxResult.Text = "生成试卷......";
            this.comboBoxHowMany.Visible = false;
            this.labelHowMany.Visible = false;
            this.buttonStart.Visible = false;

            this.buttonEndReview.Visible = true;
            this.labelReview.Visible = true;

            this.labelChinese.Visible = false;
            this.textBoxEnglish.Visible = false;
            this.buttonNext.Visible = false;
            this.buttonPrior.Visible = false;

            this.textBoxResult.Visible = true;
            this.labelFinalResult.Visible = false;
            this.buttonReturnStart.Visible = false;
            this.textBoxResult.Clear();
            try
            {
                foreach (Answer item in this.answerArr)
                    this.textBoxResult.Text += item.ToString();
            }catch(System.NullReferenceException)
            {
                this.textBoxResult.Text = "连接数据库失败";
                ToStateFinish();
            }
        }

        /// <summary>
        /// 开始测试阶段，
        /// 将会设置各个组件的可见性，模拟页面转换的效果
        /// </summary>
        private void ToStateTest()
        {
            this.labelChinese.Text = wordNum.ToString() + '/' + wordTotal.ToString()
                                        + "\r\n" + answerArr[0].Chinese;
            this.textBoxEnglish.Text = "";

            this.comboBoxHowMany.Visible = false;
            this.labelHowMany.Visible = false;
            this.buttonStart.Visible = false;

            this.buttonEndReview.Visible = false;
            this.labelReview.Visible = false;

            this.textBoxEnglish.Visible = true;
            this.labelChinese.Visible = true;
            this.buttonNext.Visible = true;
            this.buttonPrior.Visible = true;

            this.textBoxResult.Visible = false;
            this.labelFinalResult.Visible = false;
            this.buttonReturnStart.Visible = false;

        }
        /// <summary>
        /// 测试结束阶段，
        /// 将会设置各个组件的可见性，模拟页面转换的效果，
        /// 并批改测试结果，将测试批改结果输出到文本框中
        /// </summary>
        private void ToStateFinish()
        {
            this.textBoxResult.Text = "生成结果......";

            this.comboBoxHowMany.Visible = false;
            this.labelHowMany.Visible = false;
            this.buttonStart.Visible = false;

            this.buttonEndReview.Visible = false;
            this.labelReview.Visible = false;

            this.labelChinese.Visible = false;
            this.textBoxEnglish.Visible = false;
            this.buttonNext.Visible = false;
            this.buttonPrior.Visible = false;

            this.textBoxResult.Visible = true;
            this.labelFinalResult.Visible = true;
            this.buttonReturnStart.Visible = true;

            testAllWord();
            this.textBoxResult.Clear();
            foreach (string item in this.resultArr)
                this.textBoxResult.Text += item + "\r\n";
        }

        /// <summary>
        /// （命名有点不规范）
        /// 将用户填写的结果存储到相关数组中
        /// </summary>
        /// <param name="mark"></param>
        /// <param name="filled"></param>
        private void testOneWord(int mark, string filled)
        {
            wordFilledArr[mark] = filled;
        }
        /// <summary>
        /// 批改，
        /// 将结果写入相关数组中
        /// </summary>
        private void testAllWord()
        {
            for(int i = 0; i < wordTotal; i++)
            {
                if (wordFilledArr[i].Equals(answerArr[i].English))
                    resultArr[i] = "正确\r\n";
                else if(wordFilledArr[i].Length > 0)
                    resultArr[i] = "错误，错误答案：" + wordFilledArr[i] + "\r\n";
                else
                    resultArr[i] = "未填\r\n";
                resultArr[i] += answerArr[i].English + ": " + answerArr[i].Chinese + "\r\n\r\n";
                
            }
        }

    }
}
